<?php


set_include_path(APP . 'vendors' . PATH_SEPARATOR . get_include_path());
include_once 'xls/XmlSpreadsheet.php';


class XmlSpreadsheetTest extends CakeTestCase {

	var $XmlSpreadsheetWorksheet = null;
	var $XmlSpreadsheet = null;

	function startTest() {
		$this->XmlSpreadsheetWorksheet = new XmlSpreadsheetWorksheet('test');
		$this->XmlSpreadsheet = new XmlSpreadsheet();
	}

	function testInstance() {
		$this->assertTrue($this->XmlSpreadsheetWorksheet instanceof XmlSpreadsheetWorksheet);
		$this->assertTrue($this->XmlSpreadsheet instanceof XmlSpreadsheet);
		$this->assertEqual($this->XmlSpreadsheetWorksheet->getName(), 'test');
		$worksheet = '<Worksheet ss:Name="test"><Table></Table></Worksheet>';
		$this->assertEqual($this->XmlSpreadsheetWorksheet->render(), $worksheet);
	}

	function testCreateWorksheet() {
		$worksheet = '<Worksheet ss:Name="test"><Table>'
			. '<Row ss:Index="1"><Cell ss:Index="1" ><Data ss:Type="String">A1</Data></Cell><Cell ss:Index="2" ><Data ss:Type="Number">15.2</Data></Cell></Row>'
			. '<Row ss:Index="2"><Cell ss:Index="1" ><Data ss:Type="String">A2</Data></Cell></Row>'
			. '</Table></Worksheet>';
		$this->XmlSpreadsheetWorksheet->setValue(2, 1, 'A2');
		$this->XmlSpreadsheetWorksheet->setValue(1, 2, 15.2);
		$this->XmlSpreadsheetWorksheet->setValue(1, 1, 'A1');
		$this->XmlSpreadsheetWorksheet->setValue(0, 1, 'Wrong');
		$result = $this->XmlSpreadsheetWorksheet->render();
		$this->assertError('Wrong cell coordinates: (0, 1)');
		$this->assertEqual($result, $worksheet);
	}

	function testCreateSpreadsheet() {
		$spreadsheet = '<?xml version="1.0"?>' . "\n" . '<?mso-application progid="Excel.Sheet"?>' . "\n" 
			. '<Workbook'
			. ' xmlns="urn:schemas-microsoft-com:office:spreadsheet"'
			. ' xmlns:o="urn:schemas-microsoft-com:office:office"'
			. ' xmlns:x="urn:schemas-microsoft-com:office:excel"'
			. ' xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"'
			. ' xmlns:html="http://www.w3.org/TR/REC-html40">'
			. '<Styles>'
			. '<Style ss:ID="test102b094f4c3c7c27feb98b98b5171fb67"><Font ss:Bold="1"/></Style>'
			. '</Styles>'
			. '<Worksheet ss:Name="test1"><Table>'
			. '<Row ss:Index="1"><Cell ss:Index="1" ss:StyleID="test102b094f4c3c7c27feb98b98b5171fb67"><Data ss:Type="String">A1</Data></Cell></Row>'
			. '<Row ss:Index="2"><Cell ss:Index="1" ><Data ss:Type="String">A2</Data></Cell></Row>'
			. '</Table></Worksheet>'
			. '</Workbook>'
			;
		$this->XmlSpreadsheet->createWorksheet('test1');
		$this->XmlSpreadsheet->setActiveWorksheet(1);
		$this->assertError('Worksheet doesn\'t exists');
		$this->XmlSpreadsheet->setActiveWorksheet(0);
		$this->XmlSpreadsheet->getActiveSheet()->setValue(2, 1, 'A2');
		$this->XmlSpreadsheet->getActiveSheet()->setValue(1, 1, 'A1');
		$this->XmlSpreadsheet->getActiveSheet()->setStyle(1, 1, array('Font' => array('Bold' => 1)));
		$result = $this->XmlSpreadsheet->render();
		$this->assertEqual($result, $spreadsheet);

	}

	function endTest() {
		unset($this->XmlSpreadsheetWorksheet);
	}
}

?>
